﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Globalization;
public partial class Main_Reports_Inventory_ConsumptionReport_ItemWise : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    DataTable DtComprehensiveItem = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btngetreport_Click(object sender, EventArgs e)
    {
        GetReportItemWIse();
    }     
    protected void GetReportItemWIse()
    {
        try
        {
            if (txtfromdate.Text != "" && txttodate.Text != "")
            {                
                SqlParameter[] ParamItem = { new SqlParameter("@fromdate",Convert.ToDateTime(txtfromdate.Text).ToString(string.Format("yyyy/MM/dd"))),
                                   new SqlParameter("@todate",Convert.ToDateTime(txttodate.Text).ToString(string.Format("yyyy/MM/dd")))
                               };
                DtComprehensiveItem = DU.getdatausingprocedure("IVR_RPT_CONSUMPTION_ITEMWISE_REPORT", ParamItem);
                if (DtComprehensiveItem.Rows.Count > 0)
                {
                    GrdComprehensiveItemWise.DataSource = DtComprehensiveItem;
                    GrdComprehensiveItemWise.DataBind();
                }
                lblRecordNo.Text = GrdComprehensiveItemWise.Rows.Count.ToString() + " Record(s)";
                lblperiod.Text = "Period :" + txtfromdate.Text + " to " + txttodate.Text;
                ddlColumnname.Style["display"] = "block";
                lblcolumnName.Style["display"] = "block";
                lblvalue.Style["display"] = "block";
                txtValue.Style["display"] = "block";
                btnSearch.Style["display"] = "block";
                imgbtnPrint.Style["display"] = "block";
                ImgBtnExcel.Style["display"] = "block";
            }
        }
        catch (Exception ex)
        {
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GrdComprehensiveItemWise.Visible = true;
        GetReportItemWIse();
        string Filter = "";
        if (ddlColumnname.SelectedValue == "1")//for Item Code
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ITMCODE like'%" + txtValue.Text + "%'");
            GrdComprehensiveItemWise.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "2")//for Item name
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ITMDESC Like'%" + txtValue.Text + "%'");
            GrdComprehensiveItemWise.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "3")//For Bin Ref
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "BINREF Like'%" + txtValue.Text + "%'");
            GrdComprehensiveItemWise.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "4")//For Project
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "PROJECTNAME Like'%" + txtValue.Text + "%'");
            GrdComprehensiveItemWise.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "5")//For Slip No
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "SLIPNO='" + txtValue.Text + "'");
            GrdComprehensiveItemWise.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "6")//Item Group
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ITEMGROUP='" + txtValue.Text + "'");
            GrdComprehensiveItemWise.AllowPaging = false;
        }
        else
        {
            GetReportItemWIse();
        }

        DataRow[] Dr = DtComprehensiveItem.Select(Filter);
        DataTable Tempdt = new DataTable();
        Tempdt = DtComprehensiveItem.Clone();
        foreach (DataRow drNew in Dr)
        {
            Tempdt.ImportRow(drNew);
        }
        GrdComprehensiveItemWise.DataSource = Tempdt;
        GrdComprehensiveItemWise.DataBind();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
    protected void ImgBtnExcel_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ConsumptionItemWise_Report.xls"));
            Response.ContentType = "application/ms-excel";
            GrdComprehensiveItemWise.ShowFooter = false;
            GrdComprehensiveItemWise.AllowPaging = false;
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            GrdComprehensiveItemWise.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }
}